function unique(arr) {
	var out=[], obj={};
	for (var i=0; i<arr.length; i++) {
		obj[arr[i]]=0;
	}
	for (i in obj) {
		out.push(i);
	}
	return out;
}
function checkEmail($email){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test($email.val())) {
		return false;
	}
	return true;
}  
function emailHelper(req, responseFn){
	var emails = ['@gmail.com','@163.com', '@126.com', '@qq.com', '@hotmail.com', '@souhu.com', '@sina.com','@yahoo.com'];
	var source = [];
	for(var i=0;i<emails.length;i++){
		var at = req.term.indexOf('@');
		if(at == -1){
			source[i] = req.term+emails[i];
		}else{
			var pre = req.term.substring(0,at);
			var sub = req.term.substring(at); 
			var match = false;
			for(var j=0;j<emails.length;j++){
				if(emails[j].indexOf(sub)==0){ 
					source[i] = pre + emails[j];
					match = true;
				}
			}
			if( !match ) source[i] = req.term;
		}
	}   
	responseFn(unique(source));
}

function login(){
	var email = $("#login-items input[name=email]").val();
	var password = $("#login-items input[name=password]").val();
	var remember = $("#login-items input[name=remember]").is(":checked");
	
	$.post("/auth/login",{"email":email, "password":password},function(data){
		if(data.status=="0"){
			var menu = '';
			menu += '<li class="settings" onclick="toggleSetting(event,this)" ><a href="#"><img src="'+data.profile_url+'"></a></li>' 
			menu += '<li class="space"><a href="/myspace">'+data.user+'<span class="dropdown"></span></a></li>';
			menu += '<li class="divider"></li>';
			$(".topbar ul.r").html(menu); 
			
			$("#login-modal").addClass("hidden");  
		}else{
			alert(data.status);
		}
	});
}
function register(){
	var email = $("#register-items input[name=email]").val();
	var name = $("#register-items input[name=name]").val();
	var password = $("#register-items input[name=password]").val();
	
	$.post("/register",{"email":email, "password":password,'name':name},function(data){
		if(data.status=="0"){  
			$("#register-modal").addClass("hidden");  
		}else{
			alert(data.status);
		}
	});
}

function toggleModal(event,target,modalId,ajaxUrl,refresh){ 
	var $target = $(target), $modal = $("div#"+modalId);
	if($modal.length==0){
		$('<div id="' + modalId + '" class="hidden"></div>').appendTo(document.body);
		$modal = $("div#"+modalId);
		$modal.click(function(event){ 
			event.stopPropagation();
		}); 
	} 
	
	$target.toggleClass("active");
	$modal.toggleClass("hidden");
	if( $modal.hasClass("hidden") ) return;
	
	$(document).one('click', function(event) {   
		$modal.addClass("hidden"); 
		$target.removeClass("active");
	});  
	
	if(refresh || $modal.html()==""){
		$.get(ajaxUrl,function(data){   
			$modal.html(data);   
		});  	
	}
	event.stopPropagation();
}

function toggleMessage(event,target,refresh){   
	toggleModal(event,target,"message-modal","/msg",false); 
}
function toggleSetting(event,target,refresh){   
	toggleModal(event,target,"settings-modal","/settings",false); 
}

function toggleLogin(event,target){ 
	toggleModal(event,target,"login-modal","/auth/login",false);   
	$("#login-items input[name='email']").focus();
}

function toggleRegister(event,target){ 
	toggleModal(event,target,"register-modal","/register",false); 
}

function selectTopbar(){ 
	var name = window.location.pathname .split("/")[1];  
	$(".topbar li").removeClass("active");
	$('.topbar li a[href="/'+name+'"]').parent().addClass("active");
}

/* begin of weibo function */
function comment(streamId){
	if( $("#"+streamId+" .comments").length > 0 ){
		$("#"+streamId+" .comments").remove();
		return;
	}
	$.get('/myspace/cmt',{'id':streamId},function(data){
		$("#"+streamId+" .body").append(data);;
	});
}
function weibonavClicked(target){
	$current = $(target);
	$weibonav = $current.parent();
	if($current.hasClass("active")) return; 
	$("li.active",$weibonav).removeClass("active");
	$current.addClass("active");
	
}
function filter0Clicked(target){ 
	$target = $(target);
	if($target.hasClass("active")) return;
	$filter = $target.parent().parent().parent(); 
	$(".filter1 ul",$filter).toggleClass("hidden"); 
	$target.parent().children(".active").removeClass("active");
	$target.addClass("active");
}
function filter1Clicked(target){ 
	$target = $(target);
	if($target.hasClass("active")) return;
	$filter = $target.parent();  
	$filter.children(".active").removeClass("active");
	$target.addClass("active");
}
function filter2Clicked(target){ 
	$target = $(target);
	if($target.hasClass("active")) return;
	$filter = $target.parent();  
	$filter.children(".active").removeClass("active");
	$target.addClass("active");
}
function filter1DownClicked(target){ 
	$target = $(target);
	$filter = $target.parent().parent(); 
	$(".filter2",$filter).toggleClass("hidden");
	$target.toggleClass("down");
}
/* end of weibo function */

/* editor */
function initEditor(editorId){
	var editor = new baidu.editor.ui.Editor({
        id: editorId,
        UEDITOR_HOME_URL:'http://localhost/promise/editor/',
        iframeCssUrl :'http://localhost/promise/editor/themes/simple/iframe.css' ,
        minFrameHeight:200,
        toolbars: [
            ['FontFamily','FontSize'],
            ['Bold','Italic','Underline','ForeColor','BackColor'],
            ['JustifyLeft','JustifyCenter','JustifyRight'],
            ['InsertOrderedList','InsertUnorderedList'],
            ['Emoticon','PlaceName','Link','Unlink','|','Undo','Redo'],
            ['FullScreen']
        ],
        // 重写ui.Editor的renderToolbarBoxHtml方法
        renderToolbarBoxHtml: function (){
            return '<div class="%%-toolbarinner-left"><table>' +
                    '<tr><td><div class="%%-toolbarinner-lefttop">' + this.toolbars[0].renderHtml() + '</div></td><td rowspan="2"><span class="%%-toolbarinner-separate"></span></td></tr>' +
                    '<tr><td><div class="%%-toolbarinner-leftbottom">' + this.toolbars[1].renderHtml() + '</div></td></tr>' +

                '</table></div>' +
                '<div class="%%-toolbarinner-center"><table>' +
                    '<tr><td><div class="%%-toolbarinner-centertop">' + this.toolbars[2].renderHtml() + '</div></td><td rowspan="2"><span class="%%-toolbarinner-separate"></span></td></tr>' +

                    '<tr><td><div class="%%-toolbarinner-centerbottom">' + this.toolbars[3].renderHtml() + '</div></td></tr>' +

                '</table></div>' +
                '<div class="%%-toolbarinner-right">' + this.toolbars[4].renderHtml() + '</div>' +
                '<div class="%%-toolbarinner-fullscreen">' + this.toolbars[5].renderHtml() + '</div>' +
                '<div style="clear: both;overflow: hidden;height: 0;"></div>';
        }
    });
    editor.render();
    return editor;
}
$(function(){ 
	selectTopbar();  
	
	$(".bottombar .fill").click(function(event){
		toggleMessage(event,this);
	});
	
	
	$(".hover-tip").mouseleave(function(event){
		$(".hover-tip").css({'display':'none','opactiy': '0'});  
	});
	 
	$(".topbar-left li").click(function(){
		$(".topbar-left li").removeClass("active");
		$(this).addClass("active");
	});
});
 